﻿using ProjScan.Tools;
using System;
using UnityEngine;

namespace ProjScan.Submodules.Scene_Check
{
    internal class SceneStatisticsCollector : Singleton<SceneStatisticsCollector>
    {
        private GameObject[] gameObjects;

        private int triangleCount = 0;

        private int vertexCount = 0;

        private int colliderTriangleCount = 0;

        private int bakeTypeLightCount = 0;

        private int audioListenerCount = 0;

        private bool hasHighQualityCamera = false;

        public void GatherData()
        {
            gameObjects = UnityEngine.Object.FindObjectsOfType<GameObject>();
            GameObject[] array = gameObjects;
            foreach (GameObject gameObject in array)
            {
                Camera component = gameObject.GetComponent<Camera>();
                if (component != null && (component.actualRenderingPath == RenderingPath.DeferredShading || component.actualRenderingPath == RenderingPath.DeferredLighting))
                {
                    hasHighQualityCamera = true;
                    break;
                }
            }

            GameObject[] array2 = gameObjects;
            foreach (GameObject gameObject2 in array2)
            {
                AudioListener component2 = gameObject2.GetComponent<AudioListener>();
                MeshFilter component3 = gameObject2.GetComponent<MeshFilter>();
                Light component4 = gameObject2.GetComponent<Light>();
                if (component3 != null && component3.sharedMesh != null)
                {
                    triangleCount += component3.sharedMesh.triangles.Length / 3;
                    vertexCount += component3.sharedMesh.vertexCount;
                }

                MeshCollider component5 = gameObject2.GetComponent<MeshCollider>();
                if (component5 != null && component5.sharedMesh != null)
                {
                    colliderTriangleCount += component5.sharedMesh.triangles.Length / 3;
                }

                if (component4 != null && component4.lightmapBakeType == LightmapBakeType.Realtime && component4.shadows > LightShadows.None)
                {
                    bakeTypeLightCount++;
                }

                if (component2 != null)
                {
                    audioListenerCount++;
                }
            }
        }

        public void ClearData()
        {
            Array.Clear(gameObjects, 0, gameObjects.Length);
            triangleCount = 0;
            vertexCount = 0;
            colliderTriangleCount = 0;
            bakeTypeLightCount = 0;
            audioListenerCount = 0;
            hasHighQualityCamera = false;
        }

        public int GetTriangleCount()
        {
            return triangleCount;
        }

        public int GetVertexCount()
        {
            return vertexCount;
        }

        public int GetColliderTriangleCount()
        {
            return colliderTriangleCount;
        }

        public int GetBakeTypeLightCount()
        {
            return bakeTypeLightCount;
        }

        public bool HasHighQualityCamera()
        {
            return hasHighQualityCamera;
        }

        public int GetAudioListenerCount()
        {
            return audioListenerCount;
        }
    }
}